package banco.composEstoque;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import utilidades.FormatarData;
import banco.Banco;
import dominio.Cliente;
import dominio.composEstoque.*;
import fachada.composEstoque.Fachada_Demonstrativo_Cons_Imob;

public class Demonstrativo_Cons_Imob_DAO implements Fachada_Demonstrativo_Cons_Imob {

	/* (non-Javadoc)
	 * @see banco.composEstoque.Fachada_Demonstrativo_Cons_Imob#insertDem_Cons_Imob(dominio.Cliente, dominio.composEstoque.Demonstrativo_Cons_Imob)
	 */
	@SuppressWarnings("finally")
	public void insertDem_Cons_Imob(Cliente c, Demonstrativo_Cons_Imob dem_cons_imob, String filial) throws ClassNotFoundException, IOException {
	        
	        Connection conn = null;
	        PreparedStatement stmt = null;
	        ResultSet rs = null;
	        
	        try {
	            conn = Banco.getConexaoEXTRATOPAES();
	            stmt = conn.prepareStatement("insert into composestoque_dem_cons_imob values (?, ?, ?, ?, ?, ?)");
	            stmt.setString(1, FormatarData.formatar(dem_cons_imob.getData()));
	            stmt.setInt(2, dem_cons_imob.getNumero_nf());
	            stmt.setFloat(3, dem_cons_imob.getValor());
	            stmt.setString(4, dem_cons_imob.getTipo().toString());
	            stmt.setString(5, c.getCnpj().trim());
	            stmt.setString(6, filial);
	            stmt.executeUpdate();
	        }
	        catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        
	        catch (Exception e) {
	        	e.printStackTrace();
	        }
	        finally {
		        Banco.closeConexao(conn, rs, stmt);
	        }
	    }

	@SuppressWarnings("finally")
	public Collection selectDem_Cons_Imob(Cliente c, String tipo, String filial) throws IOException {
		
		Connection conn = null;
	    PreparedStatement stmt = null;
	    ResultSet rs = null;
	    Demonstrativo_Cons_Imob cons_imob;
	    Collection<Demonstrativo_Cons_Imob> res = new ArrayList<Demonstrativo_Cons_Imob>();
	    
	    try {
	        conn = Banco.getConexaoEXTRATOPAES();
	        stmt = conn.prepareStatement("select * from composestoque_dem_cons_imob where cnpj_cliente = ? and tipo = ? and filial = ?");
	        stmt.setString(1, c.getCnpj().trim());
	        stmt.setString(2, tipo);
	        stmt.setString(3, filial);
	        rs = stmt.executeQuery();
	        while (rs.next()){
	        	cons_imob = new Demonstrativo_Cons_Imob();
	        	cons_imob.setData(FormatarData.inserirData(rs.getString("data")));
	        	cons_imob.setData_string(rs.getString("data"));
	        	cons_imob.setNumero_nf(rs.getInt("numero_nf"));
	        	cons_imob.setPeriodo(FormatarData.getMesAno(rs.getString("data")));
	        	cons_imob.setTipo(Cons_Imob.setCons_Imob(rs.getString("tipo")));
	        	cons_imob.setValor(rs.getFloat("valor"));
	        	res.add(cons_imob);
	        }
	    }
	    catch (SQLException ex) {
	        ex.printStackTrace();
	    }
	    finally {
	        Banco.closeConexao(conn, rs, stmt);
	        return res;
	    }
	    
	}
	
}
